import cryptoJS from 'crypto-js'

export default class SigV4Utils {
    sign(key, msg) {
        var hash = cryptoJS.HmacSHA256(msg, key);
        return hash.toString(cryptoJS.enc.Hex);
    }

    sha256(msg) {
        var hash = cryptoJS.SHA256(msg);
        return hash.toString(cryptoJS.enc.Hex);
    }

    getSignatureKey(key, dateStamp, regionName, serviceName) {
        var kDate = cryptoJS.HmacSHA256(dateStamp, 'AWS4' + key);
        var kRegion = cryptoJS.HmacSHA256(regionName, kDate);
        var kService = cryptoJS.HmacSHA256(serviceName, kRegion);
        var kSigning = cryptoJS.HmacSHA256('aws4_request', kService);
        return kSigning;
    }
}
